import jwt from "jsonwebtoken";

// 假设这是你的 JWT 密钥
const SECRET_KEY = "your_secret_key";

// 定义用户信息的模板
const userInfoTemplate = {
  "id|+1": 1, // 用户ID，初始值为1，每次自增1
  name: "@cname", // 中文名字
  username: "@name", // 中文名字
  email: "@email", // 随机邮箱
  phone: /^1[34578]\d{9}$/, // 随机手机号码
  orgaId: 1,
  orzId: 2,
  orgaName: "组织一",
  orzName: "队一",
};

const getUserList = (size) => {
  let list = [];
  for (let index = 0; index < size; index++) {
    list = list.concat({
      "id|+1": 1, // 用户ID，初始值为1，每次自增1
      name: "@cname", // 中文名字
      username: "@name", // 中文名字
      email: "@email", // 随机邮箱
      phone: /^1[34578]\d{9}$/, // 随机手机号码
      orgaId: 1,
      orzId: 2,
      orgaName: "组织一",
      orzName: "队一",
    });
  }
  return list;
};

// 生成 JWT 令牌的函数
function generateJWTToken() {
  // 这里只是示例，实际使用中可能需要包含更多信息
  return jwt.sign(userInfoTemplate, SECRET_KEY, { expiresIn: "1h" }); // 有效期为1小时
}

export default [
  {
    url: "/api/user/login",
    method: "get",
    response: () => {
      return {
        code: 200,
        data: { ...userInfoTemplate, access_token: generateJWTToken() },
      };
    },
  },
  {
    url: "/api/user/info",
    method: "get",
    response: () => {
      return {
        code: 200,
        data: { ...userInfoTemplate },
      };
    },
  },
  {
    url: "/api/user/permission",
    method: "get",
    response: () => {
      return {
        code: 200,
        data: [
          {
            href: "/home",
            icon: "icon iconfont icon-home",
            operate: "",
            pid: "0",
            id: "1",
            show: true,
            sort: 1,
            title: "首页",
            type: 1,
          },
        ],
      };
    },
  },
  {
    url: "/api/user/list",
    method: "get",
    response: (options) => {
      const urlParams = new URLSearchParams(options.url.split("?")[1] || "");
      const list = getUserList(urlParams.get("pageSize"));
      return {
        code: 200,
        total: 12,
        data: list,
      };
    },
  },
];
